package com.jwds.digital.bios.business.service.rule.impl;

import com.jwds.digital.bios.business.core.constants.RuleCodeConstant;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchParameterReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.FiveBasicInformationReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.digital.bios.business.service.rule.DispatchRuleSortInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预配料仓停止
* */
@Slf4j
@Component
public class PreIngredientWarehouseStopped implements DispatchRuleSortInterface {
    @Autowired
    private BasicStockBinFeign basicStockBinFeign;
    @Override
    public String getRuleCode() {
        return RuleCodeConstant.PRE_INGREDIENT_WAREHOUSE_STOPPED;
    }

    @Override
    public List<DispatchParameterReqDTO> sortProcess(List<DispatchParameterReqDTO> dataList) {
        List<DispatchParameterReqDTO> resultList = new ArrayList<>();
        List<BasicStockBinRespVO> list = new ArrayList<>();
        List<FiveBasicInformationReqDTO> flowList = new ArrayList<>();
        if (dataList != null){
            for (DispatchParameterReqDTO dispatchParameterReqDTO : dataList) {
                if (dispatchParameterReqDTO.getFlowList()!=null&&dispatchParameterReqDTO.getSlot()!=null&&dispatchParameterReqDTO.getDeviceCodeList()!=null&&dispatchParameterReqDTO.getSlotIdentification()!=null){
                    for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO : dispatchParameterReqDTO.getFlowList()) {
                        if (fiveBasicInformationReqDTO.getOtherSlotList()!=null&&fiveBasicInformationReqDTO.getFlow()!=null&&fiveBasicInformationReqDTO.getDeviceCodeList()!=null) {
                            for (BasicStockBinRespVO s : fiveBasicInformationReqDTO.getOtherSlotList()) {
                                BasicStockBinReqDTO binReqDTO = new BasicStockBinReqDTO();
                                binReqDTO.setGroupCode(s.getGroupCode());
                                //        查询仓的所有信息
                                list.addAll(FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(binReqDTO)));
                            }
                        }else {
                            log.info("流程传过来数据为空");
                        }
                    }
                    for (FiveBasicInformationReqDTO fiveBasicInformationReqDTO : dispatchParameterReqDTO.getFlowList()) {
                        for (BasicStockBinRespVO basicStockBinRespVO : list) {
                            int result = basicStockBinRespVO.getInventory().compareTo(basicStockBinRespVO.getUpperLimit());
                            if (result>=0){
                                FiveBasicInformationReqDTO dto = new FiveBasicInformationReqDTO();
                                dto.setOtherSlotList(list);
                                dto.setFlow(fiveBasicInformationReqDTO.getFlow());
                                dto.setDeviceCodeList(fiveBasicInformationReqDTO.getDeviceCodeList());
                                dto.setOtherSlotDefault(fiveBasicInformationReqDTO.getOtherSlotDefault());
                                dto.setDeviceTypeDefault(fiveBasicInformationReqDTO.getDeviceTypeDefault());
                                flowList.add(dto);
                            }
                        }
                    }
                    DispatchParameterReqDTO dispatchConfiguratReqDTO = new DispatchParameterReqDTO();
                    dispatchConfiguratReqDTO.setFlowList(flowList);
                    dispatchConfiguratReqDTO.setSlot(dispatchParameterReqDTO.getSlot());
                    dispatchConfiguratReqDTO.setDeviceCodeList(dispatchParameterReqDTO.getDeviceCodeList());
                    dispatchConfiguratReqDTO.setSlotIdentification(dispatchParameterReqDTO.getSlotIdentification());
                    dispatchConfiguratReqDTO.setDeviceCodeDefault(dispatchParameterReqDTO.getDeviceCodeDefault());
                    dispatchConfiguratReqDTO.setState(dispatchParameterReqDTO.getState());
                    resultList.add(dispatchConfiguratReqDTO);
                }else {
                    log.info("流程传过来数据为空");
                }
            }
        }else {
            log.info("流程传过来数据为空");
        }
        return resultList;
    }
}
